package com.armani.controller;

import com.alibaba.fastjson.JSON;
import com.armani.entity.User;
import com.armani.model.Result;
import com.armani.model.UserModel;
import com.armani.service.UserService;
import com.armani.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: armani
 * @Package: com.armani.controller
 * @ClassName: UserController
 * @Description: TODO
 * @Author: Roc
 * @Date: 2021/11/28 15:44
 * @Version: 1.0
 **/
@Controller
@RequestMapping(value = "/user", produces = "application/json; charset=utf-8")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(value = "/list/search.do")
    @ResponseBody
    public String ListSearch(@RequestParam(required = false) Integer level,
                             @RequestParam(required = false) String user,
                             @RequestParam(required = false) Integer page,
                             @RequestParam(required = false) Integer limit) {
        Integer startRow = (page - 1) * limit;
        List<User> users = userService.ListSearch(level, user, startRow, limit);
        Integer count = userService.getCount(level, user);

        Result result = new Result();
        result.setData(users);
        result.setCount(count);
        result.setCode(0);
        result.setMsg("确认");
        String json = JSON.toJSONString(result);
        return json;
    }

    @PostMapping(value = "/insert.do")
    @ResponseBody
    public String insert(@RequestParam(required = false) String userName, @RequestParam(required = false) String user,
                         @RequestParam(required = false) String password, @RequestParam(required = false) String phone,
                         @RequestParam(required = false) String sex, @RequestParam(required = false) String cardId,
                         @RequestParam(required = false) String birth, @RequestParam(required = false) String email,
                         @RequestParam(required = false) String createtime, @RequestParam(required = false) Integer level,
                         @RequestParam(required = false) BigDecimal balance,
                         @RequestParam(required = false) Integer zhuangtai) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date births = sdf.parse(birth);
        Date createtimes = sdf.parse(createtime);
        userService.insert(userName, user, password, phone, sex, cardId, births, email, createtimes,
                level, balance, zhuangtai);

        Result result = new Result();
        result.setCount(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    @PostMapping(value = "/update.do")
    @ResponseBody
    public String update(@RequestParam String userName, @RequestParam String user, @RequestParam String password,
                         @RequestParam String phone, @RequestParam String sex, @RequestParam String cardId,
                         @RequestParam String birth, @RequestParam String email,
                         @RequestParam String createtime, @RequestParam Integer level,
                         @RequestParam BigDecimal balance, @RequestParam Integer zhuangtai,
                         @RequestParam Integer id) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date births = sdf.parse(birth);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createtimes = sdf1.parse(createtime);
        userService.update(userName, user, password, phone, sex, cardId, births, email, createtimes,
                level, balance, zhuangtai, id);

        Result result = new Result();
        result.setCount(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    @PostMapping(value = "/delete.do")
    @ResponseBody
    public String delete(@RequestParam Integer id) {
        userService.delete(id);

        Result result = new Result();
        result.setCount(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    @GetMapping(value = "/ztupdate.do")
    @ResponseBody
    public String ztupdate(@RequestParam String zhuangtai,
                           @RequestParam Integer id) {
        userService.ztupdate(zhuangtai, id);

        Result result = new Result();
        result.setCount(0);
        result.setMsg("成功");
        String json = JSON.toJSONString(result);
        return json;
    }

    /**
     * 用户登录
     *
     * @param userModel 用户
     * @return 用户信息JSON格式
     */
    @PostMapping(value = "/login.do")
    @ResponseBody
    public String login(@RequestBody UserModel userModel) {
        User users = userService.login(userModel);
        Result result = new Result();
        if (users != null) {
            result.setCode(0);
            result.setData(users);
            result.setMsg("登录成功");
        } else {
            result.setCode(1000);
            result.setMsg("账号或密码错误");
        }
        String json = JSON.toJSONString(result);
        return json;
    }

    /**
     * 用户注册
     * @param userModel 用户信息参数
     * @return
     */
    @PostMapping(value = "register.do")
    @ResponseBody
    public String register(@RequestBody UserModel userModel) {
        Integer row = userService.register(userModel);
        Result result = new Result();
        if (row != 0) {
            result.setCode(0);
//            result.setData(user);
            result.setMsg("注册成功！");
        } else {
            result.setCode(1000);
            result.setMsg("失败！");
        }
        return JsonUtil.stringify(result);
    }
}
